package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.convert.HotelOrderConverter;
import com.travel.convert.RentCarOrderConverter;
import com.travel.dto.HotelOrderDto;
import com.travel.dto.RentCarOrderDto;
import com.travel.mapper.OrderMapper;
import com.travel.pojo.Order;
import com.travel.service.OrderSender;
import com.travel.service.OrderService;
import com.travel.util.JedisPoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.HashMap;

/**
* @author rjq51
* @description 针对表【order】的数据库操作Service实现
* @createDate 2023-05-18 10:38:38
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    private OrderSender orderSender;


    @Override
    public Page<Order> queryHotelOrder(int i, int i1, int status,Integer userInfoFromRedis) {
        //mybatisplus根据用户id分页查询
        Page<Order> page = new Page<>(i, i1);
        QueryWrapper<Order> eq = new QueryWrapper<Order>()
                .eq("user_id", userInfoFromRedis)
                .eq("order_type", 1);
        if (status != 0) {
            eq.eq("status", status);
        }
        Page<Order> pageList = orderMapper.selectPage(page,eq);
        return pageList;
    }

    @Override
    public Page<Order> queryRentCarOrder(int i, int i1, int status,Integer userInfoFromRedis) {
        //mybatisplus根据用户id分页查询
        Page<Order> page = new Page<>(i, i1);
        QueryWrapper<Order> eq = new QueryWrapper<Order>()
                .eq("user_id", userInfoFromRedis)
                .eq("order_type", 4);
        if (status != 0) {
            eq.eq("status", status);
        }
        Page<Order> pageList = orderMapper.selectPage(page, eq);
        return pageList;
    }

    @Override
    public HashMap<String, String> generateOrder(HotelOrderDto hotelOrderDto, Integer userId) {
        return null;
    }

    @Override
    public HashMap<String, String> generateHotelOrder(HotelOrderDto hotelOrderDto, Integer userId) {
        Jedis jedis = JedisPoolUtil.getJedisPoolInstance().getResource();
        String orderId = jedis.get(hotelOrderDto.getOrderId());
        HashMap<String, String> result = new HashMap<>();
        if (orderId == null) {
            result.put("code", "400");
            result.put("msg", "订单ID已过期");
            return result;
        }
        // 生成订单
        Order order = HotelOrderConverter.INSTANCE.entityToDto(hotelOrderDto);
        order.setOrderType(1);
        extracted(userId, orderId, result, order);
        result.put("subject",order.getHotelRoomType());
        return result;
    }

    @Override
    public HashMap<String, String> generateRentCarlOrder(RentCarOrderDto rentCarOrderDto, Integer userId) {
        Jedis jedis = JedisPoolUtil.getJedisPoolInstance().getResource();
        String orderId = jedis.get(rentCarOrderDto.getOrderId());
        HashMap<String, String> result = new HashMap<>();
        if (orderId == null) {
            result.put("code", "400");
            result.put("msg", "订单ID已过期");
            return result;
        }
        // 生成订单
        Order order = RentCarOrderConverter.INSTANCE.entityToDto(rentCarOrderDto);
        order.setOrderType(4);
        extracted(userId, orderId, result, order);
        result.put("subject",order.getCarName());
        return result;
    }

    private void extracted(Integer userId, String orderId, HashMap<String, String> result, Order order) {
        order.setId(orderId);
        order.setUserId(userId);
        result.put("code", "200");
        result.put("msg", "订单生成成功");
        result.put("orderId", orderId);
        result.put("totalPrice", String.valueOf(order.getTotalPrice()));
        orderMapper.insert(order);
        orderSender.sendOrder(order);
    }
}




